<?php
namespace app\mobile\controller;
use clt\Lunar;
use think\Db;
use think\facade\Request;
use Qrcode\Qrcode;
class Guamai extends Common{
    public function initialize(){
        session_start();
        parent::initialize();
    }
    public function index(){
        $lists=db('guamai')->where('is_ok',0)->order('createtime desc')->select();
		$this->assign('lists',$lists);
        return view();
    }
    public function guamai(){

        $userid=$_SESSION['think']['user']['id'];
        $server=getuser($userid);
        $id=input('id');
        //获取购买信息
        $guamailist=db('guamai')->where('id',$id)->find();
        $summoney=$guamailist['num']*$guamailist['price']/100;

        if($server['money_jifen']<$summoney){
            $result=array('success'=>0,'msg'=>'您的余额不足，请充值后再试');
            echo    json_encode($result);
        }else{
                if($guamailist['uid']==$userid){
                    $result=array('success'=>0,'msg'=>'您不能购买自己挂卖的积分');
                    echo    json_encode($result);
                }else{
                    $is_ok=db('users')->where('id',$userid)->setDec('money_jifen',$summoney);
                    if($is_ok){
                        db('users')->where('id',$userid)->setInc('money_benjin',$guamailist['num']);
                    }
                    $data=array(
                      'gid'=>$userid,
                      'is_ok'=>1,
                      'gnickname'=>$server['mobile']
                    );
                    db('guamai')->where('id',$guamailist['id'])->update($data);
                    $result=array('success'=>1,'msg'=>'购买成功');
                    echo    json_encode($result);

                }

        }
    }
}